//障碍间空隙脚本
cc.Class({
    extends: cc.Component,

    properties: {
        //小鸟对象
        Bird: {
            default: null,
            type: cc.Node,
            visible: false
        },
        //计分标记
        ScoreTag: {
            default: false,
            visible: false
        },
        //得分音效
        ScoreAudio: {
            default: null,
            type: cc.AudioSource
        }
    },

    onLoad () {
        //获取小鸟对象
        this.Bird = Game.Self.Bird;
        this.CollState = {
            coll_in: false,
            coll_out: false
        }
    },

    update (dt) {
        //if(Game.End == true) {
        //    return;
        //}
        //this.Collision_Check(this.Bird,"right");
    },

    //碰撞检测
    //[node:检测对象 dir:方向]
    Collision_Check (node,dir) {
        var self = this.node.convertToWorldSpace(this.node);
        var the_node = node.convertToWorldSpace(node);
        var width = this.node.width;
        var height = this.node.height;
        //根据锚点判断中心点
        var node_w = node.width * this.node.anchorX;
        var node_h = node.height * this.node.anchorY;
        var nodeX = the_node.x + node_w;
        var nodeY = the_node.y + node_h;
        this.CollState.coll_in = false;
        if (nodeX > self.x && nodeX < self.x + width) {
            if(nodeY > self.y && nodeY < self.y + height) {
                this.CollState.coll_in = true;
            }
        }
        switch (dir) {
            case "up":
                if(nodeX > self.x && nodeX < self.x + width) {
                    if(nodeY > self.y) {
                        this.CollState.coll_out = true;
                    }
                }
            break;
            case "down":
                if(nodeX > self.x && nodeX < self.x + width) {
                    if(nodeY < self.y + height) {
                        this.CollState.coll_out = true;
                    }
                }
            break;
            case "left":
                if(nodeY > self.y && nodeY < self.y + height) {
                    if(nodeX < self.x) {
                        this.CollState.coll_out = true;
                    }
                }
            break;
            case "right":
                if(nodeY > self.y && nodeY < self.y + height) {
                    if(nodeX > self.x) {
                        this.CollState.coll_out = true;
                    }
                }
            break;
        }
        //得分检测
        if(this.CollState.coll_in == true && this.CollState.coll_out == true) {
            if(this.ScoreTag == false) {
                Game.Score += 1;
                this.ScoreTag = true;
                this.ScoreAudio.play();
            }
            this.CollState.coll_in = false;
            this.CollState.coll_out = false;
        }
        if(this.CollState.coll_in == false && this.CollState.coll_out == true) {
            this.ScoreTag = false;
            
        }
    },

    //碰撞结束后调用
    onCollisionExit(other,self){
        var collision = Game.StrShear(other.name,"<");
        //若当前碰撞的是小鸟，则停止游戏
        if(collision == "Bird") {
            //当游戏重置时,小鸟处于PipeBox中,若在此时重置游戏
            //由于小鸟需要归位,在碰撞回调函数onCollisionExit中也属于碰撞结束后调用,
            //因此需要在这时候重置得分,否则会出现重置游戏时得分加一的情况
            if(this.Bird.y == 22 && this.Bird.x == 0) {
                Game.Score = 0;
                return;
            }
            
            Game.Score += 1;
            this.ScoreAudio.play();
        }
    },
});
